#include "gaia_common.h"
#include "cJSON.h"


int main (int argc, char * argv[]) 
{

  char *uri = "{\"username\":\"root\",\"passwd\":\" Chaoyang Road \",\"port\":22,\"sign\":\"b0bf5b31747fe7b2dc6545b089653d1b\",\"host\":\"192.168.10.153\"}";
  HOST *hostinfo;
  hostinfo = (HOST *)malloc(sizeof(HOST));
  if(hostinfo == NULL)
  {
    fprintf(stderr,"malloc error!\n");
    return 1;
  }
  pid_t pid;

  if( (pid = fork()) < 0 )
  {
    perror("fork error!\n");
    exit(1);
  }else if(pid == 0){
    gaia_parse_uri(0,uri,hostinfo);
  }else
    exit(1);

  printf("%s\n",hostinfo->passwd);
  printf("%d\n",hostinfo->port);
  printf("%s\n",hostinfo->username);

  return 0;
}


int gaia_parse_uri(int flags,const char *uri,struct hostinfo *host_info)
{
  cJSON *root;
  int value_int;
  char *value_string = NULL;

  root = cJSON_Parse(uri);
  if(flags == 0)
  {
    strcpy(host_info->username,cJSON_GetObjectItem(root,"username")->valuestring);
    host_info->port = cJSON_GetObjectItem(root,"port")->valueint;
    strcpy(host_info->passwd,cJSON_GetObjectItem(root,"passwd")->valuestring);
    strcpy(host_info->sign,cJSON_GetObjectItem(root,"sign")->valuestring);
    strcpy(host_info->host,cJSON_GetObjectItem(root,"host")->valuestring);
    host_info->hostid = 0;
    host_info->task_type = 0;
  }else if(flags == 1)
  {
    strcpy(host_info->username,"NULL");
    host_info->port = 0;
    host_info->hostid = 0;
    strcpy(host_info->passwd,"NULL");
    strcpy(host_info->sign,cJSON_GetObjectItem(root,"sign")->valuestring);
    strcpy(host_info->host,"NULL");
    host_info->task_type = 0;
  }else if(flags == 2){
    strcpy(host_info->username,"NULL");
    host_info->port = 0;
    host_info->hostid = 0;
    strcpy(host_info->passwd,"NULL");
    strcpy(host_info->sign,cJSON_GetObjectItem(root,"sign")->valuestring);
    strcpy(host_info->host,"NULL");
    host_info->task_type = cJSON_GetObjectItem(root,"task_type")->valueint;
  }

  return 0;
}
